using UnityEngine;
using System.Collections;

public class lwEZSpriteInput : MonoBehaviour, IUIObject
{
	private SpriteBase m_LinkedSprite = null;
	
	public Vector3 replacement = new Vector3( 0, 0, 0 );
	public Vector3 resize = new Vector3( 0, 0, 0 );
	
	public bool m_ColliderAutoResize = true;
	public bool ColliderAutoResize
	{
		get { return m_ColliderAutoResize; }
		set
		{
			if( m_LinkedSprite != null )
			{
				if (m_ColliderAutoResize && !value)
					m_LinkedSprite.RemoveSpriteresizedDelegate(OnResize);
				if (!m_ColliderAutoResize && value)
					m_LinkedSprite.AddSpriteResizedDelegate(OnResize);
				m_ColliderAutoResize = value;
			}	
		}
	}


	public delegate void SpriteResizedDelegate(float newWidth, float newHeight, SpriteRoot sprite);
	protected void OnResize(float newWidth, float newHeight, SpriteRoot sprite)
	{
		if(collider is BoxCollider)
		{
			Vector3[] verts = m_LinkedSprite.GetVertices();
			Vector3 min = verts[1];
			Vector3 max = verts[3];

			BoxCollider bc = (BoxCollider)collider;
			bc.size = max - min + resize;
			bc.center = min + bc.size * 0.5f + replacement - resize * 0.5f;
			bc.isTrigger = true;
		}
	}

	void Awake()
	{
		if(collider == null)
			gameObject.AddComponent<BoxCollider>();
		
		if(rigidbody == null)
			gameObject.AddComponent<Rigidbody>();
		rigidbody.isKinematic = true;
		
		m_LinkedSprite = gameObject.GetComponent<SpriteBase>();

		if (m_LinkedSprite != null && m_ColliderAutoResize)
			m_LinkedSprite.AddSpriteResizedDelegate(OnResize);
	}

	//for IUIObject inheritance
	protected bool m_controlIsEnabled = true;
	public virtual bool controlIsEnabled
	{
		get { return m_controlIsEnabled; }
		set { m_controlIsEnabled = value; }
	}
	public virtual bool DetargetOnDisable
	{
		get { return DetargetOnDisable; }
		set { DetargetOnDisable = value; }
	}
	public IUIObject GetControl(ref POINTER_INFO ptr) { return this; }

	public bool RequestContainership(IUIContainer cont) { return false; }
	public virtual bool GotFocus() { return false; }

	public virtual void OnInput(POINTER_INFO ptr)
	{
		if(inputDelegate!=null)
			inputDelegate(ref ptr);
	}
	
	protected EZInputDelegate inputDelegate;
	public virtual void SetInputDelegate(EZInputDelegate del) { inputDelegate = del; }
	public virtual void AddInputDelegate(EZInputDelegate del) { inputDelegate += del; }
	public virtual void RemoveInputDelegate(EZInputDelegate del) { inputDelegate -= del; }

	protected EZValueChangedDelegate changeDelegate;
	public virtual void SetValueChangedDelegate(EZValueChangedDelegate del) { changeDelegate = del; }
	public virtual void AddValueChangedDelegate(EZValueChangedDelegate del) { changeDelegate += del; }
	public virtual void RemoveValueChangedDelegate(EZValueChangedDelegate del) { changeDelegate -= del; }

	protected IUIContainer container;

	public virtual IUIContainer Container
	{
		get { return null; }
		set {}
	}

	//for EZDragDrop inheritance
	public void DragUpdatePosition(POINTER_INFO ptr) { }
	public void CancelDrag() { }
	public void OnEZDragDrop_Internal(EZDragDropParams parms) { }
	public void AddDragDropDelegate(EZDragDropDelegate del) { }
	public void RemoveDragDropDelegate(EZDragDropDelegate del) { }
	public void SetDragDropDelegate(EZDragDropDelegate del) { }
	public void SetDragDropInternalDelegate(EZDragDropHelper.DragDrop_InternalDelegate del) { }
	public void AddDragDropInternalDelegate(EZDragDropHelper.DragDrop_InternalDelegate del) { }
	public void RemoveDragDropInternalDelegate(EZDragDropHelper.DragDrop_InternalDelegate del) { }
	public EZDragDropHelper.DragDrop_InternalDelegate GetDragDropInternalDelegate() { return null; }

	public object Data { get{return null;} set{} }
	public bool IsDraggable { get{return false;} set { } }
	public LayerMask DropMask { get {return 0;} set { } }
	public bool IsDragging { get{return false;} set { } }
	public GameObject DropTarget { get { return null; } set { } }
	public bool DropHandled { get { return false; } set { } }
	public float DragOffset { get { return 0; } set { } }
	public EZAnimation.EASING_TYPE CancelDragEasing { get{return EZAnimation.EASING_TYPE.Default;} set { } }
	public float CancelDragDuration { get { return 0; } set { } }
}
